package com.yndsoft.jc.ui.memberpage.address;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.ui.memberpage.address.entity.AddressBean;
import com.yndsoft.jc.ui.memberpage.myorder.MemberAddress;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/12/7.
 */

public class MailingAddressAdapter extends BaseAdapter {

    private List<MemberAddress> datas = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private int selectPosition = -1;
    private CallBack myCallBack;

    public MailingAddressAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setDatas(List<MemberAddress> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void setMyCallBack(CallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        MailAddressViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_address_content, null);
            holder = new MailAddressViewHolder();
            holder.address_title = convertView.findViewById(R.id.address_title);
            holder.tv_default_address = convertView.findViewById(R.id.tv_default_address);
            holder.address_manage_phone = convertView.findViewById(R.id.address_manage_phone);
            holder.address_addressManage = convertView.findViewById(R.id.address_addressManage);
            holder.iv_change_ads = convertView.findViewById(R.id.iv_change_ads);
            holder.iv_delete_ads = convertView.findViewById(R.id.iv_delete_ads);
            holder.address_item_root = convertView.findViewById(R.id.address_item_root);
            convertView.setTag(holder);
        } else {
            holder = (MailAddressViewHolder) convertView.getTag();
        }
        AppUtils.setText(holder.address_title, datas.get(position).getName());
        AppUtils.setText(holder.address_manage_phone, datas.get(position).getPhone());
        AppUtils.setText(holder.address_addressManage, (datas.get(position).getProvince() == null ? "" : datas.get(position).getProvince())
                + (datas.get(position).getCity() == null ? "" : datas.get(position).getCity())
                + (datas.get(position).getDistrict() == null ? "" : datas.get(position).getDistrict())
                + datas.get(position).getAddress());
        if (datas.get(position).isdefault()) {
            setSelected(holder.tv_default_address);
        } else {
            setNoSelected(holder.tv_default_address);
        }

        holder.tv_default_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myCallBack.defaultClick(position);
            }
        });
        holder.address_item_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myCallBack.defaultClick(position);
            }
        });
//        if (selectPosition != -1) {
//            if (selectPosition == position) {
//                setSelected(holder.tv_default_address);
//            } else {
//                setNoSelected(holder.tv_default_address);
//            }
//        }
        holder.iv_delete_ads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCallBack.click(position);
            }
        });

        holder.iv_change_ads.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCallBack.editClick(datas.get(position));
            }
        });

        return convertView;
    }

    class MailAddressViewHolder {
        TextView address_title, tv_default_address, iv_change_ads, iv_delete_ads,
                address_manage_phone, address_addressManage;
        LinearLayout address_item_root;
    }

    public interface CallBack {

        public void click(int position);

        public void editClick(MemberAddress item);

        public void defaultClick(int position);
    }

    public void setSelected(TextView textView) {
        textView.setText("默认地址");
        textView.setTextColor(Color.parseColor("#f65b71"));
        Drawable drawable = context.getResources().getDrawable(R.mipmap.checked);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable, null, null, null);
    }

    public void setNoSelected(TextView textView) {
        textView.setText("设为默认");
        textView.setTextColor(Color.parseColor("#3c3c3c"));
        Drawable drawable = context.getResources().getDrawable(R.mipmap.unchecked);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable, null, null, null);
    }
}
